package com.hz.sdka.ada;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.anythink.nativead.api.ATNative;
import com.hz.sdka.commona.SDKContext;
import com.hz.sdka.commona.api.feed.HZFeedAdView;
import com.hz.sdka.commona.api.feed.OnHZFeedListener;
import com.hz.sdka.commona.api.feed.OnHZFeedLoadListener;
import com.hz.sdka.commona.bean.HZAdError;
import com.hz.sdka.commona.bean.HZAdInfo;
import com.hz.sdka.commona.utils.ConvertUtils;
import com.hz.sdka.sdka.FeedAda;
import com.hz.sdka.sdka.AdNativea;
import com.hz.sdka.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 信息流广告
 */
public class NativeAda {
    private String TAG = "TopOnAd";
    private static NativeAda instance = new NativeAda();
    private List<HZFeedAdView> hzFeedAdViews = new ArrayList();

    private FeedAda hzInfoFlowAd;
    private int count = 1;//信息流缓存个数
    private AdListenera adListener;
    private ViewGroup viewGroup;
    private String placeId;
    private String scenarioId;
    private int layoutId;

    public static NativeAda getInstance() {
        return instance;
    }

    public void load(String placeId) {
        Log.i(TAG, "loadAd " + hzFeedAdViews.size());
        if (hzFeedAdViews.size() >= count) return;
        if (hzInfoFlowAd == null)
            hzInfoFlowAd = new FeedAda(SDKContext.getInstance().getContext(), placeId);
        hzInfoFlowAd.setListener(new OnHZFeedLoadListener() {
            @Override
            public void onFeedLoadFailed(HZAdError hzAdError) {
                Log.i(TAG, "onFeedLoadFailed " + hzAdError.printStackTrace());
                if (adListener != null) adListener.adError(hzAdError);
            }

            @Override
            public void onFeedLoaded(AdNativea nativeAdSDK) {
                Log.i(TAG, "onFeedLoaded");
                if (adListener != null) adListener.adLoaded();
                if (nativeAdSDK != null) hzFeedAdViews.add(nativeAdSDK);
                if (NativeAda.this.viewGroup != null) {
                    showAd(viewGroup, placeId, scenarioId, layoutId);
                }
                loadAd();
            }
        });
        if (adListener != null) adListener.adLoading();
        loadAd();
    }

    /*渲染试图*/
    public void showAd(ViewGroup viewGroup, String placeId, String scenarioId) {
        showAd(viewGroup, placeId, scenarioId, R.layout.native_ad_item);
    }

    /*渲染试图*/
    public void showAd(ViewGroup viewGroup, String placeId, String scenarioId, int layoutId) {
        if (!TextUtils.isEmpty(scenarioId)) {
            ATNative.entryAdScenario(placeId, scenarioId);
        }
        if (adListener != null) adListener.adRequest();
        Log.i(TAG, "showAd");
        if (hzFeedAdViews.size() > 0) {
            HZFeedAdView hzFeedAd = hzFeedAdViews.get(0);
            if (hzFeedAd != null) {
                if (adListener != null) adListener.adReady();
                hzFeedAd.setListener(new OnHZFeedListener() {
                    @Override
                    public void onFeedShown(HZAdInfo hzAdInfo) {
                        Log.i(TAG, "onFeedShown");
                        hzFeedAdViews.remove(0);
                        if (adListener != null) adListener.adShow();
                        NativeAda.this.viewGroup = null;
                        loadAd();
                    }

                    @Override
                    public void onFeedClosed() {
                        Log.i(TAG, "onFeedClosed");
                        viewGroup.setVisibility(View.GONE);
                        if (adListener != null) adListener.adClose();
                    }

                    @Override
                    public void onFeedClicked() {
                        Log.i(TAG, "onFeedClicked");
                    }

                });
                View feedView = hzFeedAd.getFeedView();
                if (feedView != null) {
                    ViewGroup parent = (ViewGroup) feedView.getParent();
                    if (parent != null) {
                        parent.removeAllViews();
                    }
                    viewGroup.removeAllViews();
                    viewGroup.addView(feedView);
                    int width = ConvertUtils.getScreenWidth();
                    hzFeedAd.render(layoutId, width, scenarioId);
                    viewGroup.setVisibility(View.VISIBLE);
                }
            }
        } else {
            this.viewGroup = viewGroup;
            this.placeId = placeId;
            this.scenarioId = scenarioId;
            this.layoutId = layoutId;
            loadAd();
        }
    }


    private void loadAd() {
        Log.i(TAG, "loadAd " + hzFeedAdViews.size());
        if (hzFeedAdViews.size() >= count) return;
        if (hzInfoFlowAd != null) hzInfoFlowAd.load();
    }

    public void setAdListener(AdListenera adListener) {
        this.adListener = adListener;
    }

}


